package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.CrossActivityDetail;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/28 14:23
 */
public class CrossActivityDetailDaoImpl extends TempDataObjectDao<CrossActivityDetail>{

    public Map<Integer, Map<Integer, CrossActivityDetail>> getCrossActivityDetailMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, CrossActivityDetail>> map = new ConcurrentHashMap<>();
        if (activityIdList.size() > 0) {
            try {
                getDb().setConnName(DbNameStrategy.getCross2Db());
                call = getDb().getConnection().prepareCall("select * from t_c_activity_detail where activityId in (" + StringUtils.listToString(activityIdList, ",") + ")");
                rs = call.executeQuery();
                while (rs.next()) {
                    CrossActivityDetail crossActivityDetail = parseData(rs);
                    Map<Integer, CrossActivityDetail> groupMap = map.get(crossActivityDetail.getActivityId());
                    if (null == groupMap) {
                        groupMap = new ConcurrentHashMap<>();
                        map.put(crossActivityDetail.getActivityId(), groupMap);
                    }
                    groupMap.put(crossActivityDetail.getGroupId(), crossActivityDetail);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                getDb().closeConnection(call, rs);
            }
        }
        return map;
    }

    public static String updateCrossActivityDetailSql = "update t_c_activity_detail set param1=? where activityId=? and groupId=?";
    @Override
    protected boolean updateData(CrossActivityDetail crossActivityDetail) {
        try {
            crossActivityDetail.setBeginWriteTime();
            getDb().setConnName(DbNameStrategy.getCross2Db());
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.BIGINT, crossActivityDetail.getParam1()));
            para.put(i++, new DbParameter(Types.INTEGER, crossActivityDetail.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, crossActivityDetail.getGroupId()));
            if (getDb().executeNoneQuery(updateCrossActivityDetailSql, para) > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    public static String insertCrossActivityDetailSql = "insert into t_c_activity_detail(activityId,groupId,param1) values (?,?,?)";
    @Override
    protected boolean addData(CrossActivityDetail crossActivityDetail) {
        try {
            crossActivityDetail.setBeginWriteTime();
            getDb().setConnName(DbNameStrategy.getCross2Db());
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, crossActivityDetail.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, crossActivityDetail.getGroupId()));
            para.put(i++, new DbParameter(Types.BIGINT, crossActivityDetail.getParam1()));
            if (getDb().executeNoneQuery(insertCrossActivityDetailSql, para) > 0) {
                return true;
            }
        }catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;

    }

    protected CrossActivityDetail parseData(ResultSet rs) throws SQLException {
        CrossActivityDetail crossActivityDetail = new CrossActivityDetail();
        crossActivityDetail.setActivityId(rs.getInt("activityId"));
        crossActivityDetail.setGroupId(rs.getInt("groupId"));
        crossActivityDetail.setParam1(rs.getLong("param1"));
        return crossActivityDetail;
    }
}
